package com.situ.science.controller;

import com.situ.science.pojo.Researchers;
import com.situ.science.pojo.Researchers;
import com.situ.science.pojo.vo.ResearchersVo;
import com.situ.science.service.IResearchersService;
import com.situ.science.utils.CommonUtil;
import com.situ.science.utils.JwtUtil;
import com.situ.science.utils.PageRusults;
import com.situ.science.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/researchers")
public class ResearchersController {
    @Autowired
    private IResearchersService researchersService;
    @Autowired
    RedisTemplate redisTemplate;
    @PostMapping("/login")
    public Result login(@RequestBody Researchers researchers, HttpServletRequest request) {
        System.out.println("ResearchersController.login");
        //First check whether the account password is empty
        //no,no,The front end will verify this,Just query directly.
        //写查询语句,不对,必须得写token,不然就得用session
        String captcha = (String) redisTemplate.opsForValue().get(CommonUtil.getCaptchaKey(request));
        if (StringUtils.isEmpty(captcha)) {
            return Result.error("验证码已失效");
        }
        if (!captcha.equalsIgnoreCase(researchers.getCaptcha())) {
            return Result.error("请输入正确验证码");
        }
        Researchers logeinResearchers = researchersService.login(researchers);
        if (logeinResearchers == null || logeinResearchers.getPassword() == null) {
            return Result.error("wrong username or password");
        }
        //successful login
        //已知要调用一个map，执行JwtUtil.createToken(map);
        //并且map中存放的是用户名和密码
        Map<String, Object> map = new HashMap<>();

        map.put("id", logeinResearchers.getId());
        map.put("name", logeinResearchers.getName());


        String token = JwtUtil.createToken(map);
        //现在前台点击login会返回一个状态码code和一个内容token的data
        return Result.ok("successful login",token);
    }

    @RequestMapping("/info")
    public Result info(@RequestHeader(name = "Authorization") String token) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer id = (Integer) map.get("id");
        Researchers researchers = researchersService.info(id);
        return Result.ok(researchers);
    }
    @RequestMapping("/update")
    public Result update(@RequestBody Researchers researchers) {
        researchersService.update(researchers);
        return Result.ok();
    }
    @RequestMapping("/list")
    public Result list(){
        List<Researchers> researchersList = researchersService.list();
        return Result.ok(researchersList);
    }
    @RequestMapping("/selectById/{id}")
    public Result selectById(@PathVariable int id){
        List<ResearchersVo> list = researchersService.selectById(id);
        return Result.ok(list);
    }
}
